/*
 * Created on Jun 9, 2005
 *
 */
package com.erclab.internal.xpresso.dataAccess;

import java.util.*;

import com.erclab.internal.xpresso.formelements.FormElement;
import com.erclab.internal.xpresso.forms.Form;
import com.erclab.internal.xpresso.forms.FormParameters;
import com.erclab.internal.xpresso.reports.ReportFilter;
import com.erclab.internal.xpresso.security.User;

/**
 * @author Enrique Rodriguez.
 *
 */
public abstract class QueryBuilder {
	
	/**
	 * @param query
	 * @param parentKeyValue
	 */
	public abstract String replaceParentKeyValue(String query, String parentKeyValue);

	/**
	 * @param query
	 * @param uniqueKeyValue
	 */
	public abstract void replaceUniqueKeyValue(Query q, String uniqueKeyValue);

	public abstract void addSortByToQuery(Query q, String defaultOrder, String requestedOrder);
	
	public abstract String createFormElementQuery(Form theForm, FormElement fe, FormParameters fp);

	/**
	 * @param string
	 * @param parameters
	 * @return
	 */
	public abstract String modifyLookupQuery(String string, FormParameters parameters);

	/**
	 * @param attributes
	 * @param destinationObject
	 * @param uniqueKeyName
	 * @param uniqueKeyValue
	 * @return
	 */
	public abstract Query createUpdateQuery(ArrayList attributes, String destinationObject, String uniqueKeyName, String uniqueKeyValue);

	/**
	 * @param attributes
	 * @param destinationObject
	 * @param uniqueKeyName
	 * @param uniqueKeyValue
	 * @return
	 */
	public abstract Query createInsertQuery(ArrayList attributes, String destinationObject, String uniqueKeyName, String uniqueKeyValue);

	/**
	 * @param destinationObject
	 * @param uniqueKeyName
	 * @param uniqueKeyValue
	 * @return
	 */
	public abstract Query createDeleteQuery(String destinationObject, String uniqueKeyName, String uniqueKeyValue);
	
	public void applyFiltersToQuery(Query q, ArrayList reportFilters, HashMap conditions) {
		if (conditions != null){
			for (int i =0; i < reportFilters.size(); i ++){
				ReportFilter f = (ReportFilter)reportFilters.get(i);
				f.applyFilterToQuery(q, conditions);
			}
		}
	}

	/**
	 * @param q
	 * @param user
	 */
	public abstract void replaceUserInfo(Query q, User user);

	/**
	 * @param q
	 * @return
	 */
	public abstract Query createMaxResultsQuery(Query q);

	/**
	 * @param q
	 * @param i
	 */
	public abstract void addPageNumber(Query q, int pageNumber, int recordsPerPage);

	/**
	 * @param q
	 * @param map
	 */
	public void replaceRequestParameters(Query q, HashMap parameters) {
		if (parameters != null){
			Collection c = parameters.keySet();
			Iterator i = c.iterator();
		   	while(i.hasNext()) {
				String key = (String)i.next();
				String paramValue = (String)parameters.get(key);
				key = key.replaceAll("Param", "");
				if(q.getQuery().indexOf("-param:" + key + "-") > -1){
					q.setQuery(q.getQuery().replaceAll("-param:" + key + "-", "?"));
					q.addValueToQuery(paramValue);	
				}					
		   	}
		}
		
	}

	
}
